//Do-file to create dataset for the MTFS results
//Last changed by SAO 250109

/*
FTYPE
1=MZ
2=DZ
3=Adopt
4=Bio
5=Mixed
6=Misc
miss=-9
*/

clear
set more off


use "E:\ProjData\PGS and politics\MTFSdata\MTFS.dta", clear

rename pgi_easingle EA_PGI
rename pgi_cpsingle CP_PGI
rename pgi_adventuresingle ADVENTURE_PGI
rename pgi_neurosingle NEURO_PGI
rename pgi_risksingle RISK_PGI
rename pgi_extrasingle EXTRA_PGI
rename pgi_morningsingle MORNING_PGI
rename pgi_depsingle DEP_PGI
rename pgi_heightsingle HEIGHT_PGI
rename pgi_selfhealthsingle SRH_PGI
rename pgi_activitysingle ACTIVITY_PGI
rename pgi_bmisingle BMI_PGI
rename pgi_swbsingle SWB_PGI
rename pc1 PC1
rename pc2 PC2
rename pc3 PC3
rename pc4 PC4
rename pc5 PC5
rename pc6 PC6
rename pc7 PC7
rename pc8 PC8
rename pc9 PC9
rename pc10 PC10

keep EA_PGI CP_PGI EXTRA_PGI NEURO_PGI RISK_PGI ADVENTURE_PGI MORNING_PGI SRH_PGI DEP_PGI SWB_PGI ACTIVITY_PGI BMI_PGI HEIGHT_PGI PC1-PC10 ID

tempfile PGI
save `PGI'

*Use the PNAS MTFS data and merge on PGIs
use "E:\ProjData\PGS and turnout\MTFS.dta", clear
merge 1:1 ID using `PGI'

rename BYR Byear
replace VOTE = (VOTE-1)/3
rename VOTE TurnoutSelf
rename EDYRS EduYears
replace EduYears = 22 if EduYears==20


*Own turnout
egen Turnout1 = rowmean(vote_2016_new vote_2012_new vote_2008_new vote_2004_new vote_2000_new vote_1996_new)
egen Turnout2 = rowmean(vote_2018_new vote_2014_new vote_2010_new vote_2006_new vote_2002_new vote_1998_new vote_1994_new)



***MEDIATORS***
***Neuroticism (use standardized factor score for negative emotionality)
gen NEURO = zNA


***Extraversion (use standardized factor score for positive emotionality)
gen EXTRA = zPA


***Subjective wellbeing (use standardized factor score for wellbeing)
*MAKE SURE WB MEASURES WELLBEING
zscore WB
rename z_WB SWB


***Risk attitudes (use standardized factor score for risk taking)
*MAKE SURE RT MEASURES RISK TAKING
zscore RT
rename z_RT RISK


***Cognitive ability (use standardized full IQ scores)
*MAKE SURE FUL_IQ MEASURES IQ
zscore FUL_IQ
rename z_FUL_IQ CP



*Standardize PGIs by birth year
foreach name in EA_PGI ADVENTURE_PGI CP_PGI EXTRA_PGI MORNING_PGI NEURO_PGI RISK_PGI DEP_PGI HEIGHT_PGI SRH_PGI ACTIVITY_PGI BMI_PGI SWB_PGI {
bysort Byear: egen meanPGI = mean(`name')
bysort Byear: egen sdPGI = sd(`name')
gen std`name' = (`name'-meanPGI)/sdPGI
drop meanPGI sdPGI
}


*Standardize all mediators
foreach name in EduYears NEURO EXTRA SWB RISK CP {
zscore `name'
drop `name'
rename z_`name' `name'
}


drop if PC1== .
*keep if GEN == 1
*keep if FTYPE == 1 | FTYPE == 2 | FTYPE == 4
drop if FTYPE==3

keep Turnout1 Turnout2 TurnoutSelf stdEA_PGI stdCP_PGI stdEXTRA_PGI stdNEURO_PGI stdRISK_PGI stdADVENTURE_PGI stdMORNING_PGI stdSRH_PGI stdDEP_PGI stdSWB_PGI stdACTIVITY_PGI stdBMI_PGI stdHEIGHT_PGI NEURO EXTRA SWB RISK CP MALE Byear PC1-PC10 FID EduYears GENERATION FTYPE WHITE EA_PGI ADVENTURE_PGI CP_PGI EXTRA_PGI MORNING_PGI NEURO_PGI RISK_PGI DEP_PGI HEIGHT_PGI SRH_PGI ACTIVITY_PGI BMI_PGI SWB_PGI

rename FID PairID
gen SchoolID = 0
gen sample = "mtfs"

save "E:\ProjData\PGS and politics/MTFSdata.dta", replace

